﻿<%@ WebHandler Language="C#" Class="RichECMS.IHandler.UserEmail" %>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Web;
using System.IO;
using System.Text;
using System.Collections;
using System.Net;
using System.Web.SessionState;
using System.Xml;
using BusinessLogic.ClassFactory;
using LitJson;
using Common;
using System.Text.RegularExpressions;
using RichECMS;
using DataAccess;
using ValidateCommon;
using System.Text.RegularExpressions;

namespace RichECMS.IHandler
{
    public class UserEmail : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.AddHeader("Content-Type", "application/json; charset=UTF-8");
            Hashtable result = new Hashtable();

            string strParentAccount = "";
            bool bolhasParent = false;
            string strlgName = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request["lgName"]);
            string lgQQ = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request["lgQQ"]);
            string lgEmail = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request["lgEmail"]);
            string lgPhone = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request["lgPhone"]);

            Regex reg = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
            if (!reg.IsMatch(lgEmail))
            {
                result["success"] = false;
                result["error"] = "请输入正确格式的电子邮件地址";
                context.Response.Write(
      JsonMapper.ToJson(result));
                context.Response.End();
            }

            string strmsgtitle = strlgName;
            //string strmsgchvSubTitle = strParentAccount;
            StringBuilder strmsgchvSummary = new StringBuilder("姓名:<span class='lgname'>" + strlgName + "</span><br />");
            strmsgchvSummary.Append("QQ:<span class='lgQQ'>" + lgQQ + "</span><br />");
            strmsgchvSummary.Append("Email:<span class='lgEmail'>" + lgEmail + "</span><br />");
            strmsgchvSummary.Append("电话:<span class='lgPhone'>" + lgPhone + "</span><br />");

            if (HttpContext.Current.Request["PA"] != null)
            {
                string strParentAccountTmp = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request["PA"]);
                string[] TruePA = strParentAccountTmp.Split('$');
                if (TruePA.Length == 3)
                {
                    strParentAccount = TruePA[2];
                    if (!ParamAuth.IsNumber(strParentAccount))
                    {
                        result["success"] = false;
                        result["error"] = "参数非法";
                        context.Response.Write(
              JsonMapper.ToJson(result));
                        context.Response.End();
                    }
                    else
                    {
                        bolhasParent = true;
                    }
                }
            }

            //string strmsgcontent = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request["content"]);
            int intSTID = 27;


            using (DBManager objDBManager = new DBManager())
            {
                StringBuilder emailContent = new StringBuilder();
                SectionsFactory objSectionFactory = new SectionsFactory(objDBManager);
                BusinessLogic.BusinessRules.Sections objSectionsEmail = objSectionFactory.Creator(29);
                if (objSectionsEmail != null)
                {
                    emailContent.Append(objSectionsEmail.DataItem.chvContent);
                }

                ArtSecRelationFactory objArtSecRelationFactory = new ArtSecRelationFactory(objDBManager);
                //StringBuilder strWhere = new StringBuilder();
                //strWhere.AppendFormat("select ArtSecRelation.*,Article.dtmInTime from ArtSecRelation,Article where Article.intArticleId=ArtSecRelation.intArticleId and Article.chvSubTitle={0} and ArtSecRelation.intSecArtID in (select intSecArtID from ArtSecRelation where intSectionID in({1}", strParentAccount, intSTID);
                //strWhere.Append(string.Format(",0000)) order by  ArtSecRelation.intSort desc,Article.dtmInTime desc"));
                //BusinessLogic.BusinessRules.ArtSecRelation objUserWebRelation = objArtSecRelationFactory.CreatorByFullSql(strWhere.ToString());
                BusinessLogic.BusinessRules.ArtSecRelation objUserWebRelation = objArtSecRelationFactory.NewInstance();
                if (objUserWebRelation == null)
                {
                    result["success"] = false;
                    result["error"] = "该用户名已经被使用";
                    context.Response.Write(
          JsonMapper.ToJson(result));
                    context.Response.End();
                }
                else
                {
                    objDBManager.BeginTransaction();
                    try
                    {
                        ArticleFactory objFactory = new ArticleFactory(objDBManager);
                        BusinessLogic.BusinessRules.Article objArticle = objFactory.NewInstance();
                        //插入文章表
                        objArticle.DataItem.chvKeyWords = ",,,";
                        objArticle.DataItem.chvSummary = strmsgchvSummary.ToString();
                        objArticle.DataItem.chvMainPic = "";
                        objArticle.DataItem.chvMainMiniPic = "";
                        objArticle.DataItem.txtContent = "";
                        objArticle.DataItem.dtmInTime = Convert.ToDateTime(DateTime.Now.ToString("yy/MM/dd HH:mm"));
                        objArticle.DataItem.chvTitle = strmsgtitle;
                        objArticle.DataItem.chvSubTitle = strParentAccount;
                        objArticle.DataItem.chvArticleInMan = "";
                        objArticle.DataItem.chvDescription = lgEmail;
                        objArticle.DataItem.intWSID = 1;
                        objArticle.DataItem.dtmIssueTime = Convert.ToDateTime(DateTime.Now.ToString("yy/MM/dd HH:mm"));

                        objArticle.DataItem.intRecycling = (int)Utility.ArticleState.NoRecycling;

                        objArticle.DataItem.chvVedioPath = "";
                        objArticle.DataItem.chvFilePath = "";
                        objArticle.DataItem.txtSeoTitle = "";
                        objArticle.DataItem.txtSeoKeywords = "";
                        objArticle.DataItem.txtSeodescription = "";
                        objArticle.DataItem.chvArticleSource = "";
                        objArticle.DataItem.chvAuthor = "";

                        objArticle.Insert();
                        //插入关联表
                        ArtSecRelationFactory objAsFactory = new ArtSecRelationFactory(objDBManager);
                        BusinessLogic.BusinessRules.ArtSecRelation objArtSecRelation = objAsFactory.NewInstance();
                        objArtSecRelation.DataItem.intSectionID = intSTID;
                        objArtSecRelation.DataItem.intArticleID = objArticle.DataItem.intArticleID;
                        objArtSecRelation.DataItem.intAccessCount = 0;
                        objArtSecRelation.DataItem.dtmIssueTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo));
                        objArtSecRelation.DataItem.blnIssue = (int)Utility.ArticleState.NoIssue;
                        objArtSecRelation.DataItem.blnRefers = (int)Utility.ArticleRefersState.NoRefers;

                        objArtSecRelation.DataItem.intSort = 0;
                        objArtSecRelation.DataItem.intState = (int)Utility.ArticleState.NoIssue;
                        objArtSecRelation.DataItem.intType = (int)Utility.ArticleType.Article;
                        objArtSecRelation.DataItem.intAccessCount = 0;
                        objArtSecRelation.DataItem.intVerify = 0;
                        objArtSecRelation.DataItem.intGlobalRec = 0;
                        objArtSecRelation.DataItem.intSectionRec = 0;
                        objArtSecRelation.DataItem.intGlobalTop = 0;
                        objArtSecRelation.DataItem.intSectionTop = 0;
                        objArtSecRelation.DataItem.intScroll = 0;
                        objArtSecRelation.DataItem.intWSID = 1;

                        objArtSecRelation.Insert();

                        //修改栏目状态
                        SectionsFactory objSFactory = new SectionsFactory(objDBManager);
                        BusinessLogic.BusinessRules.Sections objSections = objSFactory.Creator(intSTID);
                        objSections.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;

                        objSections.Update();

                        emailContent.Replace("$NAME$", strlgName);
                        emailContent.Replace("$LINK", "http://www.czm99.com/index.aspx?PA=" + string.Format("{0}${1}${2}${3}", CMSModule.CMSRandomModule.GetString(4, new int[] { 0 }), CMSModule.CMSRandomModule.GetString(4, new int[] { 1 }), objArtSecRelation.DataItem.intSecArtID, CMSModule.CMSRandomModule.GetString(4, new int[] { 3 })));

                        CMSModule.EmailModule objSendEmail = new CMSModule.EmailModule();
                        objSendEmail.StrAppendixPath = null;
                        objSendEmail.BolUseThird_partySMTPServer1 = true;
                        objSendEmail.StrEmailContent = emailContent.ToString();
                        objSendEmail.StrEmailFromAddress = "xuxinyixuewang@czm99.com";
                        objSendEmail.StrEmailTitle = "注册成功 徐辛易学网";
                        objSendEmail.StrMyEmailAccount = "xuxinyixuewang@czm99.com";
                        objSendEmail.StrMyPassword = "@a198501";
                        objSendEmail.StrSendToEmailAddress = lgEmail;
                        objSendEmail.StrSmtpServer = "smtp.exmail.qq.com";
                        objSendEmail.SendEmail();

                        if (bolhasParent == true)
                        {
                            objArtSecRelation = objAsFactory.Creator(Convert.ToInt32(strParentAccount));
                            if (objArtSecRelation != null)
                            {
                                objArtSecRelation.DataItem.intAccessCount = objArtSecRelation.DataItem.intAccessCount + 1;
                                objArtSecRelation.Update();

                                if (objArtSecRelation.DataItem.intAccessCount == 30)
                                {
                                    objSendEmail = new CMSModule.EmailModule();
                                    objSendEmail.StrAppendixPath = null;
                                    objSendEmail.BolUseThird_partySMTPServer1 = true;
                                    objSendEmail.StrEmailContent = "您的推广已经达到30的标准";
                                    objSendEmail.StrEmailFromAddress = "xuxinczm@126.com";
                                    objSendEmail.StrEmailTitle = "推广达到标准 徐辛易学网";
                                    objSendEmail.StrMyEmailAccount = "xuxinczm";
                                    objSendEmail.StrMyPassword = "XXR090926";
                                    objSendEmail.StrSendToEmailAddress = objArticle.DataItem.chvDescription;
                                    objSendEmail.StrSmtpServer = "smtp.126.com";
                                    objSendEmail.SendEmail();
                                }
                            }
                        }

                        //提交
                        objDBManager.Commit();

                        result["success"] = true;
                        //temp["atitle"] =objArticle.DataItem.chvTitle;
                        context.Response.Write(
              JsonMapper.ToJson(result));
                    }
                    catch (Exception ex)
                    {
                        //Alert(ex.ToString().Replace("\r\n", " "));
                        objDBManager.Rollback();

                        result["success"] = false;
                        result["error"] = ex.Message;
                        //temp["atitle"] =objArticle.DataItem.chvTitle;
                        context.Response.Write(
              JsonMapper.ToJson(result));
                    }
                }

            }

            /*
             string sctxt = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request["sc"]);

             StringBuilder sbWhere = new StringBuilder();
                 if (sctxt.Trim() != "")
                 {
                     sbWhere.AppendFormat(" and Article.chvTitle like '%{0}%'", sctxt);
                 }

             try
             {
              using (ArticleFactory objFactory = new ArticleFactory())
                     {
                         StringBuilder strWhere = new StringBuilder();
                         strWhere.Append("select *  from Article where 1=1 " + sbWhere + " order by intSort desc ,dtmInTime desc,intArticleID desc");
                    
                         strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " Article.* from Article inner  join ArtSecRelation on Article.intArticleID = ArtSecRelation.intArticleID and Article.intState = '" + Request["type"].ToString().Trim().Replace("'","")+ "' " + " where ArtSecRelation.intSectionID=" + State.intSTID.ToString() + State.strWhereSql + " order by Article.intSort desc ,Article.dtmInTime desc,Article.intArticleID desc)t order by intSort asc, dtmInTime asc ,intArticleID asc)m " + " order by intSort asc, dtmInTime asc ,intArticleID asc)mm  order by intSort desc, dtmInTime desc ,intArticleID desc");
                    
                         BusinessLogic.BusinessRules.Article objArticle = objFactory.CreatorByFullSql(strWhere.ToString());
                         if (objArticle != null)
                         {
                             List<Hashtable> reslist = new List<System.Collections.Hashtable>();
                              for (int i = 0; i < objArticle.Count; i++)
                         {
                             Hashtable temp = new Hashtable();
                             temp["aid"] = objArticle.DataItem.intArticleID;
                             temp["atitle"] =objArticle.DataItem.chvTitle;
     temp["atime"] =objArticle.DataItem.dtmInTime.ToString("MM-dd");
                             reslist.Add(temp);
                         }
                          result["list"] = reslist;
                         }
                    
                     }

                 context.Response.Write(
                JsonMapper.ToJson(result));
             }
             catch (Exception error)
             {
             }
             */
        }



        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}